#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "key.h"
#include "OLED.h"
#include "PWM.h"

int main(void)
{
	OLED_Init();
	Key_init();
	PWM_Init();
	
	OLED_ShowString(1, 1, "Level:");
	OLED_ShowNum(1, 7, 0, 4);
	
	OLED_ShowString(2, 1, "Close");
	
	uint8_t isOpen = 0;
	uint16_t level = 0;
	
	while(1) {
//		for(int i = 0; i < 1000; i ++) {
//			PWM_OutPut1_level(i);
//			PWM_OutPut2_level(i);
//			OLED_ShowNum(1, 7, i, 4);
//		}
//		for(int i = 1000; i >= 0; i --) {
//			PWM_OutPut1_level(i);
//			PWM_OutPut2_level(i);
//			OLED_ShowNum(1, 7, i, 4);
//		}
		uint8_t keyNum = Key_getNum();
		if (keyNum == 1) {
			if (isOpen) {
				for(int i = 500; i >= 0; i --) {
					level = i * 2;
					PWM_OutPut1_level(level);
					PWM_OutPut2_level(level);
					OLED_ShowNum(1, 7, level, 4);
				}
				OLED_ShowString(2, 1, "Close");
				isOpen = 0;
			} else {
				for(int i = 0; i <= 250; i ++) {
					level = i * 4;
					PWM_OutPut1_level(level);
					PWM_OutPut2_level(level);
					OLED_ShowNum(1, 7, level, 4);
				}
				OLED_ShowString(2, 1, "Open ");
				isOpen = 1;
			}
		}
	}
}
